@extends('layouts.app')

@section('content')

    <div id="app">

        <div class="col-md-12">
            <div class="panel panel-default">
                <div class="panel-heading">异常用户列表</div>

                <div class="panel-body">

                    @if($users->count() > 0)
                        <table class="table">
                            <thead>
                            <tr>
                                <th>id</th>
                                <td>学号</td>
                                <td>更新时间</td>
                                <td>操作</td>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($users as $user)
                                <tr>
                                    <th>{{ $user->id }}</th>
                                    <td>{{ $user->number }}</td>
                                    <td>{{ $user->updated_at }}</td>
                                    <td><button class="btn btn-primary btn-xs" data-toggle="modal" data-target="#userModal" v-on:click="getForm('{{ $user->number }}', '{{ $user->open_id }}')">填充数据</button></td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    @endif

                </div>
            </div>
        </div>

        <div class="modal fade" id="userModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">补充资料</h4>
                    </div>
                    <div class="modal-body">
                        <template v-if="!modalLoading">
                            <img :src="codeUrl" alt="" v-on:click="codeUrl='{{ url('admin/user/exception/code') }}/' + form.sessionId + '/' + Math.random() + '/' + form.cookieId">
                                <input type="text" v-model="code">
                        </template>
                        <p v-else>加载中...</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" id="modal_close" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" id="modal_ok" v-on:click="handle" data-loading-text="保存中..." autocomplete="off">确定</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->

    </div>


@endsection

@section('js')
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                modalLoading: false,
                codeUrl: '',
                code: '',
                form: {}
            },
            methods: {
                getForm: function(number, openId){
                    this.openId = openId;
                    this.modalLoading = true;
                    this.$http.get('{{ url('admin/user/exception/getForm') }}/' + number).then(function(response){
                        app.form = response.data;
                        app.codeUrl = '{{ url('admin/user/exception/code') }}/' + app.form.sessionId + '/1/' + app.form.cookieId;
                        app.modalLoading = false;
                    });
                },
                handle: function(){
                    var $btn = $("#modal_ok").button('loading');
                    this.form.openId = this.openId;
                    this.form.txtSecretCode = this.code;
                    this.$http.post('{{ url('admin/user/exception/bind') }}', this.form).then(function(response){
                        $btn.button('reset');
                        if(response.data.code == 200){
                            document.getElementById('modal_close').click();
                            location.reload();
                        }else{
                            alert(response.data.msg);
                        }
                    });
                }
            }
        })
    </script>
@endsection